package com.qfedu.dao.impl;

import com.qfedu.dao.ManagerDao;
import com.qfedu.entity.Managers;
import com.qfedu.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @version v1.0
 * @Date 2023/10/9 17:50
 * @Author DARKER
 * @Email 1402216045@qq.com
 */
public class ManagerDaoImpl implements ManagerDao {
    @Override
    public void insert(Managers manager) {
        String sql = "insert into tb_managers(login_name,login_pwd,mgr_name,mgr_gender,mgr_tel,mgr_email,mgr_qq,create_time) " +
                "values(?,?,?,?,?,?,?,?)";
        Connection conn = DBUtil.getConn();
        PreparedStatement psmt = null;
        try {
            psmt = conn.prepareStatement(sql);
            psmt.setString(1,manager.getLoginName());
            psmt.setString(2,manager.getLoginPwd());
            psmt.setString(3,manager.getMgrName());
            psmt.setString(4,manager.getMgrGender());
            psmt.setString(5,manager.getMgrTel());
            psmt.setString(6,manager.getMgrEmail());
            psmt.setString(7,manager.getMgrQq());
            psmt.setObject(8,new Date());
            //执行sql
            psmt.executeUpdate();
            conn.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }

    }




    @Override
    public void update(Managers manager) {
        String sql = "update tb_managers set login_name=?,login_pwd=?,mgr_name=?,mgr_gender=?,mgr_tel=?," +
                "mgr_email=?,mgr_qq=?,create_time=? where mgr_id=? ";
        Connection conn = DBUtil.getConn();
        PreparedStatement psmt = null;
        try {
            psmt = conn.prepareStatement(sql);
            psmt.setString(1,manager.getLoginName());
            psmt.setString(2,manager.getLoginPwd());
            psmt.setString(3,manager.getMgrName());
            psmt.setString(4,manager.getMgrGender());
            psmt.setString(5,manager.getMgrTel());
            psmt.setString(6,manager.getMgrEmail());
            psmt.setString(7,manager.getMgrQq());
            psmt.setObject(8,manager.getCreateTime());
            psmt.setInt(9,manager.getMgrId());
            //执行sql
            psmt.executeUpdate();
            conn.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void delete(Integer id) {
        String sql = "delete from tb_managers where mgr_id=?";
        Connection conn = DBUtil.getConn();
        PreparedStatement psmt = null;
        try {
            psmt = conn.prepareStatement(sql);
            psmt.setInt(1,id);
            //执行sql
            psmt.executeUpdate();
            conn.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void delBatch(List<Integer> ids) {

    }

    @Override
    public Managers selectByName(String name) {
        Managers manager = new Managers();
        String sql = "select * from tb_managers where login_name=?";
        Connection conn = DBUtil.getConn();
        PreparedStatement psmt = null;
        try {
            psmt = conn.prepareStatement(sql);
            psmt.setString(1,name);
            //执行sql
            ResultSet resultSet = psmt.executeQuery();
            while (resultSet.next()) {
                //给返回值赋值
                manager.setMgrId( resultSet.getInt("mgr_id"));
                manager.setLoginName( resultSet.getString("login_name"));
                manager.setLoginPwd( resultSet.getString("login_pwd"));
                manager.setMgrName( resultSet.getString("mgr_name"));
                manager.setMgrGender( resultSet.getString("mgr_gender"));
                manager.setMgrTel( resultSet.getString("mgr_tel"));
                manager.setMgrEmail( resultSet.getString("mgr_email"));
                manager.setMgrQq( resultSet.getString("mgr_qq"));
                manager.setCreateTime( resultSet.getTimestamp("create_time"));

            }
            conn.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return manager;
    }

    @Override
    public List<Managers> selectAll() {
        List<Managers> managers = new ArrayList<>();
        String sql = "select * from tb_managers";
        Connection conn = DBUtil.getConn();
        PreparedStatement psmt = null;
        try {
            psmt = conn.prepareStatement(sql);
            //执行sql
            ResultSet resultSet = psmt.executeQuery();
            while (resultSet.next()) {
                //给返回值赋值
                Managers manager = new Managers();
                manager.setMgrId( resultSet.getInt("mgr_id"));
                manager.setLoginName( resultSet.getString("login_name"));
                manager.setLoginPwd( resultSet.getString("login_pwd"));
                manager.setMgrName( resultSet.getString("mgr_name"));
                manager.setMgrGender( resultSet.getString("mgr_gender"));
                manager.setMgrTel( resultSet.getString("mgr_tel"));
                manager.setMgrEmail( resultSet.getString("mgr_email"));
                manager.setMgrQq( resultSet.getString("mgr_qq"));
                manager.setCreateTime( resultSet.getTimestamp("create_time"));
                managers.add(manager);
            }
            conn.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return managers;
    }
}
